package org.roma.ninemen;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * 
 * @author Mathias Larsson and Robin Weidelid
 *
 */
public class DatabaseHelper extends SQLiteOpenHelper {

	public static final String DATABASE_NAME = "ninemenroma.db";
	public static final String TABLE_GAME = "games";
	public static final String ID = "_id";
	public static final String GAME_NAME = "name";
	public static final String GAME_TURN = "turn";
	public static final String GAME_BLOB = "gameplan";
	
	public DatabaseHelper(Context context) {
		super(context, DATABASE_NAME, null, 1);
		
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		String CREATE_TABLE = "CREATE TABLE "+TABLE_GAME+" ("+ID+" INTEGER PRIMARY KEY NOT NULL, "
		+GAME_NAME+" text not null, "
		+GAME_TURN+" integer not null, "
		+GAME_BLOB+" BLOB NOT NULL);";
		
		db.execSQL(CREATE_TABLE);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
	}

	
	public boolean saveGame(int pos, String name, Integer turn, NineMenMorrisRules gameplan){
		ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
	    try { 
	      ObjectOutput out = new ObjectOutputStream(bos); 
	      out.writeObject(gameplan); 
	      out.close(); 
	    }
	    catch(Exception e){
	    	Log.e("object stream failed",e.toString());
	    }
		
		ContentValues values = new ContentValues();
		values.put(ID, pos);
		values.put(GAME_NAME, name);
		values.put(GAME_TURN, turn);
		values.put(GAME_BLOB, bos.toByteArray());
		
		if( getWritableDatabase().insert(TABLE_GAME, null, values) == -1){
			if (getWritableDatabase().update(TABLE_GAME, values, ID+" = ?", new String[]{ ""+pos }) <= 0){
				return false;
			}
		}
		return true;
		
	}
	
	public NineMenMorrisRules loadGame(Integer pos){
		
		Cursor c = getReadableDatabase().query(TABLE_GAME, new String[]{ ID, GAME_NAME, GAME_TURN, GAME_BLOB }, ID+" = ?", new String[]{ pos.toString() }, null, null, null);
		c.moveToFirst();
		c.getInt(c.getColumnIndex(GAME_TURN));
		
		NineMenMorrisRules object = null;
		try{
		ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(c.getBlob(c.getColumnIndex(GAME_BLOB)))); 
	      object = (NineMenMorrisRules) in.readObject(); 
	      in.close(); 
		}
		catch(Exception e){
			Log.e("load error", e.toString());
		}
		
		return object;
	}
	
	public String[] getGames(){
		String[] games = {"Empty","Empty","Empty","Empty","Empty"} ;
		Cursor c = getReadableDatabase().query(TABLE_GAME, new String[] { ID,GAME_NAME } , ID+" < 9", null, null, null, ID+" ASC");
		if (c.moveToFirst()){
		do{
			games[c.getInt(0)] = c.getString(1);
		}while(c.moveToNext());
		}
		return games;
	}
	
	
}
